﻿/* 
    Copyright (c) 2011 Microsoft Corporation.  All rights reserved.
    Use of this sample source code is subject to the terms of the Microsoft license 
    agreement under which you licensed this sample source code and is provided AS-IS.
    If you did not accept the terms of the license agreement, you are not authorized 
    to use this sample source code.  For the terms of the license, please see the 
    license agreement between you and Microsoft.
  
    To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604 
  
*/
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

// Directive for the data model.
using EnglishStudy.Entity;
using System;
using System.Diagnostics;


namespace EnglishStudy.ViewModel
{
    public class LessonItemViewModel : INotifyPropertyChanged
    {
        // LINQ to SQL data context for the local database.
        private LessonDataContext lessonDB;

        // Class constructor, create the data context object.
        public LessonItemViewModel(string lessonDBConnectionString)
        {
            lessonDB = new LessonDataContext(lessonDBConnectionString);
        }

        // UserInfo Tables
        private User _userInfo;
        public User UserInfo
        {
            get { return _userInfo; }
            set
            {
                _userInfo = value;
                NotifyPropertyChanged("UserInfo");
            }
        }

        // CountryInfo Tables
        private Country _countryInfo;
        public Country CountryInfo
        {
            get { return _countryInfo; }
            set
            {
                _countryInfo = value;
                NotifyPropertyChanged("CountryInfo");
            }
        }

        // All to-do items.
        private ObservableCollection<LessonItem> _allLessonItems;
        public ObservableCollection<LessonItem> AllLessonItems
        {
            get { return _allLessonItems; }
            set
            {
                _allLessonItems = value;
                NotifyPropertyChanged("AllLessonItems");
            }
        }


        // All word items.
        private List<WordTable> _allWords;
        public List<WordTable> AllWords
        {
            get { return _allWords; }
            set
            {
                _allWords = value;
                NotifyPropertyChanged("AllWords");
            }
        }

        // A list of all categories, used by the add task page.
        private List<LessonCategory> _categoriesList;
        public List<LessonCategory> CategoriesList
        {
            get { return _categoriesList; }
            set
            {
                _categoriesList = value;
                NotifyPropertyChanged("CategoriesList");
            }
        }

        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            lessonDB.SubmitChanges();
        }
        public void LoadUserData()
        {
        

        }
        // Query database and load the collections and list used by the pivot pages.
        public void LoadCollectionsFromDatabase()
        {

            // Specify the query for all to-do items in the database.
            var toDoItemsInDB = from LessonItem item in lessonDB.LessonItems
                                select item;

            // Query the database and load all to-do items.
            AllLessonItems = new ObservableCollection<LessonItem>(toDoItemsInDB);

            // Specify the query for all categories in the database.
            var toDoCategoriesInDB = from LessonCategory category in lessonDB.Categories
                                     select category;
            //   var allword = from WordTable word in lessonDB.WordList
            //                   select word;
            //      AllWords = lessonDB.WordList.ToList();

            // Load a list of all categories.
            CategoriesList = lessonDB.Categories.ToList();
            try
            {
                List<User> asd = lessonDB.usertable.ToList();

                UserInfo = asd[0];
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }


        // Add a to-do item to the database and collections.
        public void AddLessonItem(LessonItem newLessonItem)
        {
            // Add a to-do item to the data context.
            lessonDB.LessonItems.InsertOnSubmit(newLessonItem);

            // Save changes to the database.
            lessonDB.SubmitChanges();

            // Add a to-do item to the "all" observable collection.
            AllLessonItems.Add(newLessonItem);
        }

        // Remove a to-do task item from the database and collections.
        public void DeleteLessonItem(LessonItem lessonForDelete)
        {

            // Remove the to-do item from the "all" observable collection.
            AllLessonItems.Remove(lessonForDelete);

            // Remove the to-do item from the data context.
            lessonDB.LessonItems.DeleteOnSubmit(lessonForDelete);

            // Save changes to the database.
            lessonDB.SubmitChanges();
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
